<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
    :rules="rules"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="updateFn">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { getUserDetailAPI, updateUserInforAPI } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '员工姓名不能为空！', trigger: 'blur' }],
        password: [{ required: true, message: '员工姓名不能为空！', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      const res = await getUserDetailAPI(this.$route.query.id)
      this.userInfo = res.data
    },
    async updateUserInfor() {
      const res = await updateUserInforAPI(this.userInfo)
      this.$message({ type: 'success', message: '更新信息成功！' })
      console.log(res)
    },
    updateFn() {
      this.$refs.userForm.validate(val => {
        if (val) {
          this.updateUserInfor()
        }
      })
    }
  }
}
</script>
